{% extends "base.html" %}

{% block pagetitle %}Search{% endblock %}

{% block pageheader %}Search{% endblock %}

{% block content %}

<form action="" method="get" class="well form-inline" id="search-form">
    <div>
	<input type="hidden" name="is_advanced" value="1"/>
	{{ searchform.query }}
	<input type="submit" value="Search" class="btn btn-primary"/>
    </div>
    <div class="controls">
	<label class="checkbox inline">{{ searchform.match_whole_words }}{{ searchform.match_whole_words.label }}</label>
	<label class="checkbox inline">{{ searchform.match_summary }}{{ searchform.match_summary.label }}</label>
	<label class="checkbox inline">{{ searchform.match_line }}{{ searchform.match_line.label }}</label>
	<label class="checkbox inline">{{ searchform.match_explanation }}{{ searchform.match_explanation.label }}</label>
	<label class="checkbox inline">{{ searchform.match_limitations }}{{ searchform.match_limitations.label }}</label>
    </div>
</form>

{% include 'main/elements/oneliners_searchresults.html' %}

{% endblock %}

{% block ready %}
	var i18n_search_error = 'An error occurred during search.';

	function search_success(html) {
	    $('.oneliners').html($(html).html());
	    bind_comments_toggle();
	}

	function search_error(err) {
	    $('#search-errors').text(i18n_search_error);
	}

	function search_completed() {
	    $('#search-loading').empty();
	}

	$('#search-form').submit(function(e) {
	    e.preventDefault();
	    $('#search-loading').append($('#templates .search-loading').html());
	    $('#search-errors').empty();

	    var options = {
		url: '{% url oneliners.ajax.search %}',
		type: 'get',
		data: { 
		    'is_advanced': 'on',
		    'match_whole_words': $('#id_match_whole_words:checked').val(),
		    'match_summary': $('#id_match_summary:checked').val(),
		    'match_line': $('#id_match_line:checked').val(),
		    'match_explanation': $('#id_match_explanation:checked').val(),
		    'match_limitations': $('#id_match_limitations:checked').val(),
		    'query': $('#search-form #id_query').val()
		},
		success: search_success,
		error: search_error,
		complete: search_completed
	    };
	    $.ajax(options);
	});

	$('#search-form #id_query').focus();
{% endblock %}
